package com.bjsxt.filter;

import com.bjsxt.pojo.Users;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class UserLoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //获取用户访问的URI
        HttpServletRequest req = (HttpServletRequest)servletRequest;
        String uri = req.getRequestURI();
        //判断当前访问的URI是否是用户登录资源，如果是则放行
        if(uri.indexOf("login") != -1 || uri.indexOf("userLogin") != -1){
            filterChain.doFilter(servletRequest, servletResponse);
        }else{
            //用户是否登录的判断
            HttpSession session = req.getSession();
            Users user = (Users)session.getAttribute("user");
            if(user != null && user.getUsername().length() > 0){
                filterChain.doFilter(servletRequest, servletResponse);
            }else{
                req.setAttribute("msg", "请登录");
                req.getRequestDispatcher("/login").forward(servletRequest, servletResponse);
            }
        }


    }

    @Override
    public void destroy() {

    }
}
